package com.neusoft.weixin.service.account;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.common.util.UUIDGenerator;
import com.neusoft.weixin.repository.account.ContentDao;
import com.neusoft.weixin.utils.FreeMarkerUtil;

@Service(value = "contentService")
public class ContentService extends BaseService {
	
	@Autowired
	private ContentDao contentDao;

	@Override
	public List<Map> doGridData(RowBounds rowbounds,
			Map<String, Object> condition) {
		return contentDao.getGridData(rowbounds, condition);
	}
	
	@Transactional(rollbackFor=Exception.class)
	public JSONObject addOrEditContent(JSONObject job) throws Exception{
		String contentid = job.getString("CONTENT_ID");
		if(contentid==null||contentid.isEmpty()){
			contentid = UUIDGenerator.getUUID();
			job.put("CONTENT_ID", contentid);
			contentDao.insertContent(job);
			job.put("CONTENT_ID", contentid);
			job.put("status", "1");
			job.put("detail", "添加内容成功!");
			
			return job;
		}else{
			contentDao.updateContent(job);
			job.put("status", "2");
			job.put("detail", "编辑内容成功!");
			return job;
		}
	}
	
	public void deleteContent(String content_id) throws Exception{
		
		contentDao.deleteContent(content_id);
		
	}
	
	public String createHtml(Map<String,String> map,HttpServletRequest request) throws Exception{
		
		String fileName = FreeMarkerUtil.createHtml(map, request);
		
		return fileName;
	}
	

}
